<?php
/**
 * 《产品报价》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/MoldUtil.class.php');

class Quote extends MoldUtil {
	public $name = '报价信息';
	public $tbl = Constant::tbl_mold_quote;
	
	public $type = array(
		'-' => '请选择',
		'1' => '产品',
		'2' => '注塑',	

		'9' => '其它',
	);
	
	public $payment = array(
		'-' => '请选择',
		'预付20%' => '预付20%',
		'预付30%' => '预付30%',
		'预付50%' => '预付50%',

		'预付全款' => '预付全款',			
		'货到付款' => '货到付款',
	
		'当月结算' => '当月结算',
		'月结30天' => '月结30天',
		'月结60天' => '月结60天',
		'月结90天' => '月结90天',

		'其它' => '其它',
	);
			
	public $status = array(
		'-' => '请选择',
		'1' => '候选报价',
		'2' => '已经下单',
		'3' => '正在生产',
		'4' => '完成生产',
		'5' => '正在结算',
		'6' => '完成结算',
		'7' => '完成对帐',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '报销人',
		'date' => '报销日期',
		'status' => '状态',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'emp' => '报销人',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('payment_option', $this->payment);
				
		$controller->assign_by_ref('type', json_encode($this->type));		
		$controller->assign_by_ref('type_option', $this->type);
		
		$controller->assign_by_ref('currency', json_encode($this->currency));		
		$controller->assign_by_ref('currency_option', $this->currency);
	}
	
	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tab($controller, $request) {		
		$data = array(
			'外发加工' => array('cc'=>'mold.php?do=Process.find', 'js'=>''),
			'加工明细' => array('cc'=>'mold.php?do=ProcessItem.find', 'js'=>''),
		
			'产品信息' => array('cc'=>'mold.php?do=Mold.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t1', strtotime('-2 month'));

		$controller->display($request, 'mold/quote/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'date') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['date'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {	
		$name_s = $request['name_s'];
		
		$where = " WHERE 1=1";
				
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		if (!empty($payment_s) && $payment_s <> '-') {
			$where .= " AND payment = '" . $payment_s . "'";
		}		
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		} else {
			$controller->clear_assign('result');
		}
				
		$controller->display($request, 'mold/quote/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'mold/process/list');
		return true;
	}
		
	function _batch_insql($request) {		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		return $insql;
	}
	
	function _num($id) {
		$ret = $this->_quote_item($id);
		
		return count($ret);
	}	

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));
		
		$row['item'] = json_encode($this->_item_blank());		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/quote/add');
		return true;
	}
	
	function _item_blank($len = 5) {
		$ret = array();
		$i = 0;
		for ($i = 0; $i < $len; $i++) {
			$ret[] = array("ok"=>true);
		}
		
		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$date = $request['date'];
		$emp = $this->_input($request['emp']);
		
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$type = $request['type'];
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$amount = $request['amount'];
		$currency = $request['currency'];
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(supplier, supplier_x, type, name, date, currency, amount, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $supplier, $supplier_x, $type, $name, $date, $currency, $amount, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$id = $db->insert_id();
		
		$request['id_s'] = $id;
		$request['id'] = $id;
		
		$this->_insert_item($request);		

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _insert_item($request) {
		require_once(dirname(__FILE__) . '/QuoteItem.class.php');
		$aa = new QuoteItem();
		
		return $aa->_insert($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_quote_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/quote/edit');
		return true;
	}
	
	function _quote_item($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/QuoteItem.class.php');
		$aa = new QuoteItem();
		
		return $aa->_list($id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$date = $request['date'];
		$emp = $this->_input($request['emp']);
		
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$name = $request['name'];
		$content = addslashes($request['content']);
		
		$amount = $request['amount'];
		$currency = $request['currency'];
				
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET supplier = '%s', supplier_x = '%s', name = '%s', date = '%s', currency = '%s', amount = '%s', ";
		$fmt .= "content = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $supplier, $supplier_x, $name, $date, $currency, $amount, $content, $emp, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}		
		
		$this->_update_item($request);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _update_item($request) {
		require_once(dirname(__FILE__) . '/QuoteItem.class.php');
		$aa = new QuoteItem();
		
		return $aa->_update($request);
	}

	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['item'] = json_encode($this->_process_item($row['id']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/process/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		 
		$row['item'] = $this->_quote_item($row['id']);
				
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/quote/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['supplier_x'] = json_decode($row['supplier_x']);
		
		$row['payment_t'] = $this->currency[$row['payment']];
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
				
				$this->_delete_quote_item($id);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete_quote_item($id) {
		require_once(dirname(__FILE__) . '/QuoteItem.class.php');
		$aa = new QuoteItem();
		$aa->_delete_quote($id);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _ss_status($id, $status) {
		$this->_status_process_item($id, $status);
	}
	
	function verify($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['item'] = $this->_process_item($row['id']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'mold/process/verify');
		return true;
	}

	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$this->_status_process_item($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function finish($controller, $request) {
		$this->payment = $this->_shift($this->payment);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$row['item'] = $this->_process_item($row['id']);

		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'mold/process/finish');
		return true;
	}
	
	function finishPost($controller, $request) {		
		$this->pretreat($controller);
		
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$content = addslashes($request['content']);
		$status = 6;

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$this->_status_process_item($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);
		
		$this->_status_process_item($id, $status);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _status_process_item($process, $status) {
		require_once(dirname(__FILE__) . '/ProcessItem.class.php');
		$aa = new ProcessItem();

		$aa->_status_process($process, $status);
		
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {
		
		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND code LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);

		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'mold/process/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(date, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(total) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(total) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function t($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s GROUP BY supplier, t1";
		$sql = sprintf($fmt, Constant::tbl_mold_outer_piece);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$this->_process($row);
		}

		logdate($controller, $request);
		return true;
	}

	function _process($data) {
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(supplier, date, emp, oper, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s')";
		$sql = sprintf($fmt, $this->tbl, $data['supplier'], $data['t1'], $data['emp'], $data['oper'], $data['createtime'], $data['modifytime']);
		echo $sql . "\n";
		$db->query($sql);
		
		$id = $db->insert_id();
		
		$fmt = "SELECT * FROM %s WHERE supplier = '%s' AND t1 = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mold_outer_piece, $data['supplier'], $data['t1']);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$fmt = "INSERT INTO %s(process, mold, name, type, amount, date, content, emp, oper, createtime, modifytime)";
			$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')";
			$sql = sprintf($fmt, Constant::tbl_process_item, $id, $row['mold'], $row['name'], $row['type'], $row['cost'], $row['t2'], '', $row['emp'], $row['oper'], $row['createtime'], $row['modifytime']);
			echo $sql . "\n";
			$db->query($sql);
		}		
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE supplier_x is NULL";
		// $fmt = "SELECT * FROM %s WHERE length(supplier_x) < 10";
		// $fmt = "SELECT * FROM %s WHERE createtime < '2011-05-09'";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$supplier_x = $this->_supplier_x($row['supplier']);	
			$amount = $this->_amount($row['id']);		
			
			$fmt = "UPDATE %s SET supplier_x = '%s', amount = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $supplier_x, $amount, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _amount($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT SUM(amount) t FROM %s WHERE process = '%s'";
		$sql = sprintf($fmt, Constant::tbl_process_item, $id);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function x($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s ORDER BY id DESC";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$supplier_x = $this->_x($row['supplier'], $row['supplier_x']);
			
			$fmt = "UPDATE %s SET supplier_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $supplier_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _x($id, $supplier_x) {
		$j = json_decode($supplier_x);
		
		// var_dump($j);
		
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$aa = new Supplier();
		
		$c = $aa->_view($id);
				
		$ret['name'] = $c['name'];
		$ret['fullname'] = $c['fullname'];
		$ret['address'] = $c['address'];
		$ret['web'] = $c['url'];
		
		if (empty($j->contact)) {
			$ret['contact'] = $j->name;
		} else {
			$ret['contact'] = $j->contact;
		}
		
		$ret['mobile'] = $j->mobile;
		$ret['tel'] = $j->tel;
		$ret['fax'] = $j->fax;
		$ret['email'] = $j->email;		
		$ret['content'] = $j->content;
		
		$ret = json_encode($ret);
		
		return addslashes($ret);
	}
}
?>